#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100
#define MAX_COURSES 6
#define FILENAME "students.txt"

// 定义课程名称数组
const char *courses[MAX_COURSES] = {"语文", "数学", "英语", "物理", "化学", "生物"};

// 定义学生结构体，增加存储多门课程成绩的数组
typedef struct {
	char id[20];
	char name[50];
	char gender[10];
	char nation[50];
	int age;
	char course[MAX_COURSES][50];  // 存储课程名称
	float score[MAX_COURSES];     // 存储对应课程成绩
} Student;

// 全局变量，存储学生信息
Student students[MAX_STUDENTS];
int studentCount = 0;

// 从文件中加载学生信息
void loadStudents() {
	FILE *file = fopen(FILENAME, "r");
	if (file == NULL) {
		return;
	}
	studentCount = 0;
	while (studentCount < MAX_STUDENTS) {
		// 读取学号、姓名、性别、民族、年龄
		if (fscanf(file, "%s %s %s %s %d", 
			students[studentCount].id, students[studentCount].name,
			students[studentCount].gender, students[studentCount].nation,
			&students[studentCount].age) != 5) {
			break;
		}
		// 读取六门课程及成绩
		for (int i = 0; i < MAX_COURSES; i++) {
			if (fscanf(file, "%s %f", students[studentCount].course[i], &students[studentCount].score[i]) != 2) {
				break;
			}
		}
		studentCount++;
	}
	fclose(file);
}

// 将学生信息保存到文件中
void saveStudents() {
	FILE *file = fopen(FILENAME, "w");
	if (file == NULL) {
		printf("无法打开文件进行保存！\n");
		return;
	}
	for (int i = 0; i < studentCount; i++) {
		// 写入学号、姓名、性别、民族、年龄
		fprintf(file, "%s %s %s %s %d ", 
			students[i].id, students[i].name, students[i].gender, students[i].nation, students[i].age);
		// 写入六门课程及成绩
		for (int j = 0; j < MAX_COURSES; j++) {
			fprintf(file, "%s %.2f ", students[i].course[j], students[i].score[j]);
		}
		fprintf(file, "\n");
	}
	fclose(file);
}

// 增加学生信息
void addStudent() {
	if (studentCount >= MAX_STUDENTS) {
		printf("学生数量已达到上限！\n");
		return;
	}
	printf("请输入学号: ");
	scanf("%s", students[studentCount].id);
	printf("请输入姓名: ");
	scanf("%s", students[studentCount].name);
	printf("请输入性别: ");
	scanf("%s", students[studentCount].gender);
	printf("请输入民族: ");
	scanf("%s", students[studentCount].nation);
	printf("请输入年龄: ");
	scanf("%d", &students[studentCount].age);
	
	// 依次输入六门课程成绩
	for (int i = 0; i < MAX_COURSES; i++) {
		strcpy(students[studentCount].course[i], courses[i]);
		printf("请输入%s成绩: ", courses[i]);
		scanf("%f", &students[studentCount].score[i]);
	}
	
	studentCount++;
	saveStudents();
	printf("学生信息添加成功！\n");
}

// 删除学生信息，这里假设删除某个学生所有课程信息
void deleteStudent() {
	char id[20];
	printf("请输入要删除的学生学号: ");
	scanf("%s", id);
	int found = 0;
	for (int i = 0; i < studentCount; i++) {
		if (strcmp(students[i].id, id) == 0) {
			for (int j = i; j < studentCount - 1; j++) {
				students[j] = students[j + 1];
			}
			studentCount--;
			found = 1;
			saveStudents();
			printf("学生信息删除成功！\n");
			break;
		}
	}
	if (!found) {
		printf("未找到该学号的学生信息！\n");
	}
}

// 修改学生信息，这里假设修改某个学生所有课程成绩
void modifyStudent() {
	char id[20];
	printf("请输入要修改的学生学号: ");
	scanf("%s", id);
	int found = 0;
	for (int i = 0; i < studentCount; i++) {
		if (strcmp(students[i].id, id) == 0) {
			printf("请输入新姓名: ");
			scanf("%s", students[i].name);
			printf("请输入新性别: ");
			scanf("%s", students[i].gender);
			printf("请输入新民族: ");
			scanf("%s", students[i].nation);
			printf("请输入新年龄: ");
			scanf("%d", &students[i].age);
			
			// 依次输入六门课程新成绩
			for (int j = 0; j < MAX_COURSES; j++) {
				printf("请输入%s新成绩: ", courses[j]);
				scanf("%f", &students[i].score[j]);
			}
			
			found = 1;
			saveStudents();
			printf("学生信息修改成功！\n");
			break;
		}
	}
	if (!found) {
		printf("未找到该学号的学生信息！\n");
	}
}

// 查询学生人数
void queryTotalStudents() {
	printf("学生总人数: %d\n", studentCount);
}

// 按性别查询学生人数
void queryStudentsByGender() {
	char gender[10];
	printf("请输入要查询的性别: ");
	scanf("%s", gender);
	int count = 0;
	for (int i = 0; i < studentCount; i++) {
		if (strcmp(students[i].gender, gender) == 0) {
			count++;
		}
	}
	printf("性别为 %s 的学生人数: %d\n", gender, count);
}

// 按民族查询学生人数
void queryStudentsByNation() {
	char nation[50];
	printf("请输入要查询的民族: ");
	scanf("%s", nation);
	int count = 0;
	for (int i = 0; i < studentCount; i++) {
		if (strcmp(students[i].nation, nation) == 0) {
			count++;
		}
	}
	printf("民族为 %s 的学生人数: %d\n", nation, count);
}

// 统计学生的平均分数，这里计算所有课程的总平均分
void calculateAverageScore() {
	if (studentCount == 0) {
		printf("没有学生信息，无法计算平均分！\n");
		return;
	}
	float totalScore = 0;
	int totalCourses = studentCount * MAX_COURSES;
	for (int i = 0; i < studentCount; i++) {
		for (int j = 0; j < MAX_COURSES; j++) {
			totalScore += students[i].score[j];
		}
	}
	float averageScore = totalScore / totalCourses;
	printf("学生的平均分数: %.2f\n", averageScore);
}

// 给学生排榜，这里按所有课程的总分进行排名
void rankStudents() {
	if (studentCount == 0) {
		printf("没有学生信息，无法排榜！\n");
		return;
	}
	// 冒泡排序
	for (int i = 0; i < studentCount - 1; i++) {
		for (int j = 0; j < studentCount - i - 1; j++) {
			float sumScore1 = 0, sumScore2 = 0;
			for (int k = 0; k < MAX_COURSES; k++) {
				sumScore1 += students[j].score[k];
				sumScore2 += students[j + 1].score[k];
			}
			if (sumScore1 < sumScore2) {
				Student temp = students[j];
				students[j] = students[j + 1];
				students[j + 1] = temp;
			}
		}
	}
	printf("学生排名：\n");
	for (int i = 0; i < studentCount; i++) {
		printf("排名 %d: 学号 %s, 姓名 %s\n",
			i + 1, students[i].id, students[i].name);
	}
}

// 按课程统计学生分数
void queryScoresByCourse() {
	char courseName[50];
	printf("请输入要查询的课程名称: ");
	scanf("%s", courseName);
	int found = 0;
	printf("课程 %s 的学生成绩：\n", courseName);
	for (int i = 0; i < studentCount; i++) {
		for (int j = 0; j < MAX_COURSES; j++) {
			if (strcmp(students[i].course[j], courseName) == 0) {
				printf("学号 %s, 姓名 %s, 成绩 %.2f\n", students[i].id, students[i].name, students[i].score[j]);
				found = 1;
			}
		}
	}
	if (!found) {
		printf("未找到该课程的学生信息！\n");
	}
}

// 查询获得某课程的最高分的学生信息
void queryTopStudentByCourse() {
	char courseName[50];
	printf("请输入要查询的课程名称: ");
	scanf("%s", courseName);
	int found = 0;
	float maxScore = -1;
	for (int i = 0; i < studentCount; i++) {
		for (int j = 0; j < MAX_COURSES; j++) {
			if (strcmp(students[i].course[j], courseName) == 0) {
				if (students[i].score[j] > maxScore) {
					maxScore = students[i].score[j];
					found = 1;
				}
			}
		}
	}
	if (!found) {
		printf("未找到该课程的学生信息！\n");
		return;
	}
	printf("课程 %s 的最高分是 %.2f，获得该分数的学生信息如下：\n", courseName, maxScore);
	for (int i = 0; i < studentCount; i++) {
		for (int j = 0; j < MAX_COURSES; j++) {
			if (strcmp(students[i].course[j], courseName) == 0 && students[i].score[j] == maxScore) {
				printf("学号 %s, 姓名 %s, 性别 %s, 民族 %s, 年龄 %d\n",
					students[i].id, students[i].name, students[i].gender, students[i].nation, students[i].age);
			}
		}
	}
}

// 主函数
int main() {
	loadStudents();
	int choice;
	do {
		printf("\n学生信息管理系统\n");
		printf("1. 增加学生信息\n");
		printf("2. 删除学生信息\n");
		printf("3. 修改学生信息\n");
		printf("4. 查询学生人数\n");
		printf("5. 按性别查询学生人数\n");
		printf("6. 按民族查询学生人数\n");
		printf("7. 统计学生的平均分数\n");
		printf("8. 给学生排榜\n");
		printf("9. 按课程统计学生分数\n");
		printf("10. 查询获得某课程的最高分的学生信息\n");
		printf("11. 退出\n");
		printf("请输入你的选择: ");
		scanf("%d", &choice);
		switch (choice) {
		case 1:
			addStudent();
			break;
		case 2:
			deleteStudent();
			break;
		case 3:
			modifyStudent();
			break;
		case 4:
			queryTotalStudents();
			break;
		case 5:
			queryStudentsByGender();
			break;
		case 6:
			queryStudentsByNation();
			break;
		case 7:
			calculateAverageScore();
			break;
		case 8:
			rankStudents();
			break;
		case 9:
			queryScoresByCourse();
			break;
		case 10:
			queryTopStudentByCourse();
			break;
		case 11:
			printf("退出系统！\n");
			break;
		default:
			printf("无效的选择，请重新输入！\n");
		}
	} while (choice != 11);
	return 0;
}
